package quiz;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SendMessageServlet
 */
@WebServlet("/SendMessage")
public class SendMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SendMessageServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Account currentAcct = (Account) request.getSession().getAttribute("account");
		if (currentAcct == null){
			request.setAttribute("error", "You need to be logged in to challenge someone to a quiz!");
			RequestDispatcher dpatch = request.getRequestDispatcher("login.jsp");
			dpatch.forward(request, response);
			return;
		}
		
		DataBase db = (DataBase) request.getServletContext().getAttribute("db");
		if (request.getParameter("quizID") != null) {
			int quizID = Integer.parseInt(request.getParameter("quizID"));
			Quiz challengedQuiz = db.getQuiz(quizID);
			request.setAttribute("challengedQuiz", challengedQuiz);
		}
		
		if (request.getParameter("friendID") != null) {
			int friendID = Integer.parseInt(request.getParameter("friendID"));
			Account requestAccount = db.getAccount(friendID);
			request.setAttribute("friendRequestAccount", requestAccount);
		}
		
		RequestDispatcher dpatch = request.getRequestDispatcher("new-message.jsp");
		dpatch.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DataBase db = (DataBase) request.getServletContext().getAttribute("db");
		
		//check if they're logged in
		Account currentAcct = (Account) request.getSession().getAttribute("account");
		if (currentAcct == null){
			request.setAttribute("error", "You need to be logged in to send someone a message!");
			RequestDispatcher dpatch = request.getRequestDispatcher("login.jsp");
			dpatch.forward(request, response);
			return;
		}
		
		
		int messageType = Integer.parseInt(request.getParameter("messageType"));
		int fromID = currentAcct.getID();
		String content = request.getParameter("messageContent");
		
		//check if it's an announcement
		if (messageType == Message.ANNOUNCEMENT){
			Message message = new Message(fromID, -1, messageType, content, false, -1);
			db.storeMessage(message);
			return;
		}
		
		//check who they're tryna send to
		String usernameOfRecipient = request.getParameter("usernameOfRecipient");
		Account recipient = db.getAccountByUsername(usernameOfRecipient);
		if (recipient == null){
			request.setAttribute("error", "Invalid recipient username!");
			RequestDispatcher dpatch = request.getRequestDispatcher("new-message.jsp");
			dpatch.forward(request, response);
			return;
		}
		
		int toID = recipient.getID();
	
		int challengedQuizID;
		challengedQuizID = (messageType == Message.CHALLENGE) ? 
				Integer.parseInt(request.getParameter("challengedQuiz")) : -1;
		
		Message message = new Message(fromID, toID, messageType, content, false, challengedQuizID);
		db.storeMessage(message);
		
		response.sendRedirect("Profile");
	}

}
